import {IReference} from "../Base/ReferencePool/IReference";
import {TaskStatus} from "./TaskStatus";

export abstract class TaskBase implements IReference{

    get serialId(): number {
        return this._serialId;
    }

    get priority(): number {
        return this._priority;
    }

    get status(): TaskStatus {
        return this._status;
    }

    get userData(): any {
        return this._userData;
    }
    /**
     * 任务默认优先级。
     */
    public static readonly _DefaultPriority:number = 0;

    private _serialId:number;
    private _priority:number;
    private _status:TaskStatus;
    private _userData:any;

    constructor() {
        this._serialId = 0;
        this._priority = TaskBase._DefaultPriority;
        this._status = TaskStatus.Free;
        this._userData = null;
    }

    Initialize(serialId:number,priority:number):void{
        this._serialId = serialId;
        this._priority = priority;
    }

    Clear(): void {
        this._serialId = 0;
        this._priority = TaskBase._DefaultPriority;
        this._status = TaskStatus.Free;
        this._userData = null;

    }

    OnGenerate():void{
        this._status = TaskStatus.Waiting;
    }

    OnStart():void{
        this._status = TaskStatus.Running;
    }

    OnUpdate(elapseSeconds:number,realElapseSeconds:number):void{

    }


    protected OnComplete(reason:string):void{
        this._status = TaskStatus.Completed;
    }

    protected OnFailure(reason:string):void{
        this._status = TaskStatus.Failed;
    }

    OnCancel(reason:string):void{
        this._status = TaskStatus.Canceled;
    }

}